<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
session_start();
include_once("../../config.inc.php");

$_LangPath = "competition";
include_once(__Language_Path."/selectlanguage_admin.php");
include_once(__Modules_Path."/LogRecord.lib.php");
include_once(__Modules_Path."/Utility.lib.php");
include_once(__Modules_Path."/SQLUtility.lib.php");
include_once(__Modules_Path."/Tool.lib.php");
include_once(__Modules_Path."/BetLimit.lib.php");
include_once(__Modules_Path."/ChangeTime.lib.php");
include_once(__Modules_Path."/Lotto.lib.php");
include_once(__Modules_Path."/Gamble.lib.php");
include_once(__Modules_Path."/WagerResult.lib.php");
include_once(__Modules_Path."/Competition.lib.php");
include_once(__Common_Path."/Lotto.inc.php");
include(__DBConnect_File);


$_SESSION["Admin_Info"] -> Account = trim($_data["UserAcc"]);
$_SESSION["Admin_Info"] -> ID = 99999999;
$intLID = (int)$_data["LottoID"];
$intWID = (int)$_data["Web"];
$strGameDate = $_data["GameDate"];

$arrLotto = $_arrLotto[$intLID];
$intLType = (int)$arrLotto["LType"];

$strLog = "UserAcc => ".$_SESSION["Admin_Info"] -> Account."\n";
$strLog .= "Param => ".print_r($_data, true)."\n";
AddData_Log($_SESSION["Admin_Info"] -> ID, "統一新增彩期", $strLog);

include_once(__Root_Path."/k_admin/Competition/QAdd10M.php");
/*$Y = explode("-", $strGameDate);
if(!checkdate($Y[1], $Y[2], $Y[0])){
	$GLOBALS["errormessage"] = $_strCompetition["DateError"];//"賽程日期格式錯誤！";
	$GLOBALS["errorhidden"] = "\nLine: ".__LINE__.",\nPage: ".__FILE__;
	ErrMsgOutputHTML("Error", "", 1);
}

$arrGD = getLastOneGambleData($intLID);

$arrLotto = GetLottoData($intLID);
if(!$arrLotto)ErrMsgOutputHTML("LoginError", "", 1);

//===============================================================
//找出此時間點預設彩期
$strGameTime = "";
$strEndGameTime = "";
$intSerialNo = 0;
$arrTimeParam = unserialize($arrLotto["TimeParam"]);
$strNowServerTime = date("H:i:s");

$strNowTime = TimeForm($strGameDate, date("H:i:s"), "H:i:s");
$arrTimeRange = array(5 => 50, 6 => 23, 7 => 120, 8 => 84);
$arrOSerialDiff = array(5 => true, 6 => false, 7 => false, 8 => true);
$arrZero = array(5 => 2, 6 => 2, 7 => 3, 8 => 2);
$strZero = "%0".$arrZero[$intLID]."s";
$intDenominator = pow(10, $arrZero[$intLID]);

for($intNo = 1; $intNo <= $arrTimeRange[$intLID]; $intNo++){
	if($arrTimeParam["EndGameTime"][$intNo] <= $strNowTime)continue;
	$intSerialNo = str_replace("-", "", $strGameDate).sprintf($strZero, $intNo);
	$strGameTime = $arrTimeParam["GameTime"][$intNo];
	$strEndGameTime = $arrTimeParam["EndGameTime"][$intNo];

	if($arrOSerialDiff[$intLID]){
		if($arrGD["OSerialNo"] < 201000101){
			$strDays = DateForm($strGameDate, date("H:i:s"), "z");
			$strYear = DateForm($strGameDate, date("H:i:s"), "Y");
			$intOSerialNo = $strYear.sprintf("%03s", $strDays).sprintf($strZero, $intNo);
		}elseif($arrGD["OSerialNo"] % $intDenominator == $arrTimeRange[$intLID]){
			$intOSerialNo = floor($arrGD["OSerialNo"] / $intDenominator + 1).sprintf($strZero, $intNo);
		}else{
			$intOSerialNo = floor($arrGD["OSerialNo"] / $intDenominator).sprintf($strZero, $intNo);
		}

		if($intLID == 8){
			if(floor($intSerialNo / $intDenominator) - floor($arrGD["SerialNo"] / $intDenominator) > 0){
				$intOSerialNo = $arrGD["OSerialNo"] + 1;
			}else{
				$intOSerialNo = $arrGD["OSerialNo"] + ($intSerialNo - $arrGD["SerialNo"]);
			}
		}

	}else{
		$intOSerialNo = $intSerialNo;
	}
	break;
}
if($intSerialNo == 0){
	$GLOBALS["errormessage"] = $_strCompetition["OverSerialNo50"];
	ErrMsgOutputHTML("Error Over 50", "", 1);
}

//===============================================================
//若已有彩期存在，和預設彩期比較，看那個彩期最新
if(sizeof($arrGD) > 0){
	$strOldGameDate = DateForm($arrGD["GameDate"], $arrGD["GameTime"], "Y-m-d");
	$strOldGameTime = TimeForm($arrGD["GameDate"], $arrGD["GameTime"], "H:i:s");
	$strOldEndGameTime = TimeForm($arrGD["GameDate"], $arrGD["EndGameTime"], "H:i:s");

	$isOver = false;
	//已存在彩期不可大於欲新增預設彩期
	if($arrGD["SerialNo"] >= $intSerialNo) $isOver = true;

	//已存在彩期日期不可大於欲新增預設彩期日期
	if($strOldGameDate > $strGameDate) $isOver = true;

	//已存在彩期開獎時間和截止下注時間不可大於欲新增預設彩期開獎時間和截止下注時間
	if($strOldGameDate == $strGameDate){
		if($strOldEndGameTime > $strEndGameTime || $strOldGameTime > $strGameTime) $isOver = true;
	}

	if($isOver){
		$strErr = $_strCompetition["OverSerialNo"];
		$strErr = str_replace("{SerailNo}", $arrGD["SerialNo"], $strErr);
		$strErr = str_replace("{GameDate}", $strOldGameDate, $strErr);
		$strErr = str_replace("{GameTime}", $strOldGameTime, $strErr);
		$strErr = str_replace("{EndGameTime}", $strOldEndGameTime, $strErr);
		$strErr = str_replace("{NewSerailNo}", $intSerialNo, $strErr);
		$strErr = str_replace("{NewGameDate}", $strGameDate, $strErr);
		$strErr = str_replace("{NewGameTime}", $strGameTime, $strErr);
		$strErr = str_replace("{NewEndGameTime}", $strEndGameTime, $strErr);
		$GLOBALS["errormessage"] = $strErr;
		ErrMsgOutputHTML("Error Over", "", 1);
	}
}

$data["LottoID_n"] = $intLID;
$data["SerialNo_n"] = $intSerialNo;
$data["OSerialNo_n"] = $intOSerialNo;
$data["GameDate_x"] = DateForm($strGameDate, $strGameTime, "Y-m-d", "CToU");
$data["GameTime_x"] = TimeForm($strGameDate, $strGameTime, "H:i:s", "CToU");
$data["EndGameTime_x"] = TimeForm($strGameDate, $strEndGameTime, "H:i:s", "CToU");
$data["PayupDate_x"] = $strGameDate;

//刪除過期賠率表資料
if(!DeleteTodayGame(0, $intLID))ErrMsgOutputHTML("Error1", "", 1);

//將此期樂透狀態，改為已結束
if(!setGameEnd($intLID))ErrMsgOutputHTML("Error2", "", 1);

//刪除已結帳過期注單
if(!rmCompetitionEndLastDayData())ErrMsgOutputHTML("Error3", "", 1);

if(!$intGID = InsertSQL($data, "`bet_Gamble`"))ErrMsgOutputHTML("Error4", "", 1);
if(!InsertPreOdds($intLID, $intGID))ErrMsgOutputHTML("Error5", "", 1);
if(!MakeGambleFile($intLID))ErrMsgOutputHTML("Error6", "", 1);

if(!makeShowScoreFile($intLID, "", "", ""))ErrMsgOutputHTML("Error7", "", 1);*/


?>Add OK!!